﻿using System;
using System.ServiceModel.Syndication;
using System.ServiceModel.Web;
using System.Xml;
using System.Web;
using ScottLane.MediaFeeder.ObjectModel;

namespace ScottLane.MediaFeeder.WebService
{
    /// <summary>
    /// A SyndicationItem representing an Artist.
    /// </summary>
    public class ArtistSyndicationItem : SyndicationItem
    {
        private Artist artist;

        public ArtistSyndicationItem(Artist artist)
        {
            this.artist = artist;
            this.Title = new TextSyndicationContent(artist.Name);
            //this.Content = new TextSyndicationContent(artist.Name);

            foreach (string key in artist.Genres.Keys)
            {
                this.Categories.Add(new SyndicationCategory(artist.Genres[key]));
            }

            string artistUrl = Utility.BaseUrl + "/music/artists/" + HttpUtility.UrlEncode(artist.Name);
            this.Id = artistUrl;
            this.Links.Add(SyndicationLink.CreateAlternateLink(new Uri(artistUrl)));
        }

        protected override void WriteElementExtensions(XmlWriter writer, string version)
        {
            writer.WriteElementString("mf:name", artist.Name);
            writer.WriteElementString("mf:songCount", artist.Songs.Count.ToString());
            writer.WriteElementString("mf:albumCount", artist.Albums.Count.ToString());
            writer.WriteElementString("mf:duration", artist.Duration.ToString());
            writer.WriteElementString("itunes:duration", artist.Duration.ToString());
        }
    }
}
